package com.mz.mattress.admin.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.SaleListReq;
import com.mz.mattress.admin.api.resp.SaleSum;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.SaleService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Merchant;
import com.mz.mattress.api.entity.Sale;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//销售端
@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    @GetMapping(value = "/get")
    public Result<Sale> get() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Integer typeId = userJwt.getTypeId();
        return saleService.getById(typeId);
    }

    @GetMapping(value = "/headSum")
    public Result<SaleSum> headSum() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Integer typeId = userJwt.getTypeId();
        return saleService.headSum(typeId);
    }

    @GetMapping("/searchByMobile")
    public Result<List<Sale>> searchByMobile(@RequestParam String mobile) {
        return saleService.searchByMobile(mobile);
    }

    @GetMapping(value = "/{id}")
    public Result<Sale> detail(@PathVariable Integer id) {
        return saleService.getById(id);
    }

    @PostMapping
    public Result add(@RequestBody Sale sale) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        if (!userJwt.getType().equals(Constants.ROLE_SALE)) {
            return Result.fail("不是销售");
        }

        Integer saleId = userJwt.getTypeId();
        sale.setPid(saleId);

        return saleService.add(sale);
    }

    @PutMapping
    public Result update(@RequestBody Sale sale) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        if (!userJwt.getType().equals(Constants.ROLE_SALE)) {
            return Result.fail("不是销售");
        }

        Integer saleId = userJwt.getTypeId();
        sale.setPid(saleId);

        return saleService.updateById(sale);
    }

    @GetMapping("/merchantPage")
    public Result<IPage<Merchant>> listPage(SaleListReq req) {

        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        if (!userJwt.getType().equals(Constants.ROLE_SALE)) {
            return Result.fail("不是销售");
        }

        return saleService.merchantPage(req);
    }

    @GetMapping("/salePage")
    public Result<IPage<Sale>> salePage(SaleListReq req) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        if (!userJwt.getType().equals(Constants.ROLE_SALE)) {
            return Result.fail("不是销售");
        }

        Integer saleId = userJwt.getTypeId();
        req.setPid(saleId);
        return saleService.salePage(req);
    }

}
